package com.stox.ui.javafx.charting.widget;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Side;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;

import org.controlsfx.control.RangeSlider;

import com.stox.model.preference.Preference;
import com.stox.ui.javafx.charting.event.handler.InformationAxisMouseMovedEventHandler;
import com.stox.ui.javafx.charting.model.plot.AbstractPrimaryPlot;
import com.stox.ui.javafx.charting.view.ChartingView;
import com.stox.ui.javafx.charting.widget.axis.AbstractHorizontalAxis;
import com.stox.ui.javafx.charting.widget.axis.DateAxis;
import com.stox.ui.javafx.charting.widget.axis.InformationAxis;


@SuppressWarnings("rawtypes")
public class PrimaryChart<T extends AbstractPrimaryPlot> extends Chart<T> {
	
	private boolean semilog;

	private boolean equivolume;
	
	private RangeSlider rangeSlider = new RangeSlider(0,1000,0,1000);
	
	private final AbstractHorizontalAxis topHorizontalAxis = new InformationAxis();
	
	private final AbstractHorizontalAxis bottomHorizontalAxis = new DateAxis();
	
	public PrimaryChart(ChartingView chartingView) {
		super(chartingView);
		topHorizontalAxis.setChart(this);
		topHorizontalAxis.setSide(Side.TOP);
		bottomHorizontalAxis.setChart(this);
		bottomHorizontalAxis.setSide(Side.BOTTOM);
		
		setBottom(new VBox(bottomHorizontalAxis, rangeSlider));
		
		rangeSlider.setBackground(new Background(new BackgroundFill(
				Preference.instance.getChartingPreference().getDefaultBackgroundColor(), 
				new CornerRadii(0), new Insets(0))));
		ChangeListener<Number> changeListener = new ChangeListener<Number>() {
			@Override
			public void changed(
					ObservableValue<? extends Number> paramObservableValue,
					Number paramT1, Number paramT2) {
				getChartingView().setZoom(rangeSlider.getLowValue(), rangeSlider.getHighValue());
				getChartingView().update();
			}
		};
		rangeSlider.lowValueProperty().addListener(changeListener);
		rangeSlider.highValueProperty().addListener(changeListener);
		getCanvas().setOnMouseMoved(InformationAxisMouseMovedEventHandler.instance);
	}
	
	@Override
	public void update() {
		if(getFrontPlot() ==  null ) return;
		
		super.update();
		
		if(null != topHorizontalAxis)	topHorizontalAxis.update();
		if(null != bottomHorizontalAxis) 	bottomHorizontalAxis.update();
	}
	
	@Override
	public void paint() {
		getFrontPlot().paintBackground();
		super.paint();
	}
	
	public RangeSlider getRangeSlider() {
		return rangeSlider;
	}
	
	@Override
	public void setFrontPlot(T frontPlot) {
		super.setFrontPlot(frontPlot);
		getChartingView().setTitle(frontPlot.getModel().getName()+" ("+frontPlot.getModel().getCode()+")");
		frontPlot.setVolumeData();
	}
	
	public AbstractHorizontalAxis getTopHorizontalAxis() {
		return topHorizontalAxis;
	}
	
	@Override
	public T getFrontPlot() {
		return super.getFrontPlot();
	}
	
	public boolean isSemilog() {
		return semilog;
	}

	public void setSemilog(boolean semilog) {
		this.semilog = semilog;
	}
	
	public boolean isEquivolume() {
		return equivolume;
	}

	public void setEquivolume(boolean equivolume) {
		this.equivolume = equivolume;
	}


}
